/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.janalise.system;

import br.com.janalise.view.MainFrame;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.TreePath;
import br.com.janalise.util.Arquivo;

/**
 *
 * @author boskyn9
 */
public class Configurar {

    private static Configurar instance = new Configurar();

    public static void DefaultLaF(boolean selected, JFrame frame) {
        if (selected) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        SwingUtilities.updateComponentTreeUI(frame);

    }
    private File fileConfig;
    private static String pathRepositorio;

    public static String getNode(TreePath path) {
        String node = path.toString();
        node = node.replaceAll("[\\]\\[,]", "")
                .replace(pathRepositorio, "")
                .replace(".pdf", "")
                .replace(".PDF", "")
                .trim();
        return node;
    }

    public Configurar() {
        try {
            File dir = new File("./config");
            if(!dir.exists())
                dir.mkdir();
            fileConfig = new File("./config/config.properties");
            if(!fileConfig.exists())
                fileConfig.createNewFile();

            if (!fileConfig.exists()) {
                try {
                    Arquivo.gravarArquivo("repositorio", "./config/config.properties");
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Configurar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(Configurar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Configurar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public File getFileConfig() {
        return fileConfig;
    }

    public String getRepositorio() throws FileNotFoundException, IOException {
        System.out.println("[configuração] " + fileConfig.getAbsolutePath());
        Properties p = new Properties();
        InputStream fis = new FileInputStream(fileConfig);
        p.load(fis);
        fis.close();
        String path = p.getProperty("repositorio");
        System.out.println("[repositorio] " + path);
        if (path == null || path.isEmpty()) {
            File dirLivro = Arquivo.criarPastas("livrosPDF");
            setRepositorio(dirLivro.getAbsolutePath());
        }
        return path;
    }

    public void setRepositorio(String path) throws FileNotFoundException, IOException {
        Properties p = new Properties();
         if (path == null || path.isEmpty()) {
            File dirLivro = Arquivo.criarPastas("livrosPDF");
            setRepositorio(dirLivro.getAbsolutePath());
            return;
        }
        p.setProperty("repositorio", path);
        OutputStream fos = new FileOutputStream(fileConfig,true);
        p.store(fos, "");
        fos.close();

        pathRepositorio = getRepositorio();
    }

    public void setConfiguracao(Object gp, Object gf,Object gr, Object po) throws FileNotFoundException, IOException{
        Properties p = new Properties();
        p.setProperty("gp", gp+"");
        p.setProperty("gf", gf+"");
        p.setProperty("gr", gr+"");
        p.setProperty("po", po+"");
        OutputStream fos = new FileOutputStream(fileConfig,true);
        p.store(fos, "");
        fos.close();
    }

    public Float getGrauPalavra() throws FileNotFoundException, IOException{
        Properties p = new Properties();
        InputStream fis = new FileInputStream(fileConfig);
        p.load(fis);
        fis.close();
        String gp = p.getProperty("gp");
        if(gp!=null && !gp.equalsIgnoreCase(""))
            try{
                return new Float(gp);
            }catch(NumberFormatException ex){
                return null;
            }
        else
            return null;
    }

    public Float getGrauFrequecia() throws FileNotFoundException, IOException{
        Properties p = new Properties();
        InputStream fis = new FileInputStream(fileConfig);
        p.load(fis);
        fis.close();
        String gf = p.getProperty("gf");
        if(gf!=null && !gf.equalsIgnoreCase(""))
            try{
                return new Float(gf);
            }catch(NumberFormatException ex){
                return null;
            }
        else
            return null;
    }

    public Float getGrauRelevancia() throws FileNotFoundException, IOException{
        Properties p = new Properties();
        InputStream fis = new FileInputStream(fileConfig);
        p.load(fis);
        fis.close();
        String gr = p.getProperty("gr");
        if(gr!=null && !gr.equalsIgnoreCase(""))
            try{
                return new Float(gr);
            }catch(NumberFormatException ex){
                return null;
            }
        else
            return null;
    }
     public Float getPercOcorrencia() throws FileNotFoundException, IOException{
       Properties p = new Properties();
        InputStream fis = new FileInputStream(fileConfig);
        p.load(fis);
        fis.close();
        String gr = p.getProperty("po");
        if(gr!=null && !gr.equalsIgnoreCase(""))
            try{
                return new Float(gr);
            }catch(NumberFormatException ex){
                return 0f;
            }
        else
            return 0f;
    }

    public String getPathRepositorio() {
        return pathRepositorio;
    }

    @SuppressWarnings("static-access")
    public void setPathRepositorio(String pathRepositorio) {
        this.pathRepositorio = pathRepositorio;
    }

    public static Configurar getInstance() {
        return instance;
    }
}
